#!/bin/bash
#
# post.sh -- Write a post with the default Markdown template
#

TOP_DIR=$(cd $(dirname $0) && pwd)

# Necessary

people=${TOP_DIR}/../_data/people.yml

nick=`basename $0`
nick=${nick%.*}

author=`grep -B 1 -i " $nick" -ur $people | grep " name" | cut -d':' -f2 | sed -e "s/^ *//g" | sed -e "s/ *$//g"`
nick=`grep -i " $nick" -ur $people | grep " nickname" | cut -d':' -f2 | sed -e "s/^ *//g" | sed -e "s/ *$//g"`

[ -z "$author" ] && author="Your Name"
[ -z "$nick" -o "$nick" == "post" ] && nick="Your Nick Name"

title="new-post"

# If title is in Chinese, this must be set in English
slug="new-post-slug"

# Optional

description=""

group=""
album=""
tagline=""

tags=""
categories=""

args=""
[ -n "$tags" ] && args=$args" tags=\"$tags\""
[ -n "$categories" ] && args=$args" categories=\"$categories\""

[ -n "$tagline" ] && args=$args" tagline=\"$tagline\""
[ -n "$album" ] && args=$args" album=\"$album\""
[ -n "$group" ] && args=$args" group=\"$group\""
[ -n "$draft" ] && args=$args" draft=\"$draft\""

[ -n "$description" ] && args=$args" description=\"$description\""

for i in "$@"
do
	args=$args" $(echo $i\" | sed -e 's/=/=\"/g')"
done

eval rake post author="\"$author\"" nick="\"$nick\"" title="\"$title\"" slug="\"$slug\"" $args
